﻿using EntityFramework.Extensions;
using MeetingAssistant.Component.Data.EF.Interface;
using MeetingAssistant.Component.Tools;
using MeetingAssistant.CoreBLL.Service.Member.Interface;
using MeetingAssistant.Domain.Data.Repositories.Member;
using MeetingAssistant.Domain.Model.Member;
using MeetingAssistant.ViewModel.Member;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;

namespace MeetingAssistant.CoreBLL.Service.Member
{
    public class CompanyService : CoreServiceBase, ICompanyService
    {
        private readonly ICompanyRepository _companyRepository;
        

        public CompanyService(ICompanyRepository companyRepository, IUnitOfWork unitOfWork)
            : base(unitOfWork)
        {
            this._companyRepository = companyRepository;
        }
        public IQueryable<Company> Companys
        {
            get
            {
                return _companyRepository.Entities;
            }
        }

        public IList<CompanyVM> GetListCompanyVM(Expression<Func<Company, bool>> wh, int limit, int offset, out int total)
        {
            return _companyRepository.GetListCompanyVM(wh, limit, offset, out total);
        }

        public  OperationResult Insert(CompanyVM model)
        {
            try
            {
                Company oldCompany = _companyRepository.Entities.FirstOrDefault(c => c.CompanyName == model.CompanyName.Trim() || c.Code == model.Code.Trim());
                if(oldCompany != null)
                {
                    return new OperationResult(OperationResultType.Warning, "请检查公司名称或信用代码是否输入正确！");
                }
                var entity = new Company
                {
                    CompanyName = model.CompanyName,
                    Code = model.Code,
                    CompanyAddress = model.CompanyAddress,
                    Enabled = model.Enabled,
                    UpdateDate = DateTime.Now
                };
                entity.Users.Add(new User());
               
                return new OperationResult(OperationResultType.Success, "新增数据成功！");
            }
            catch
            {
                return new OperationResult(OperationResultType.Error, "操作失败，请重试！");
            }
        }

        public OperationResult Update(CompanyVM model)
        {
            try
            {
                var company = Companys.FirstOrDefault(c => c.Id == model.Id);
                if(company == null)
                {
                    throw new Exception();
                }
                var other = Companys.FirstOrDefault(c => c.Id != model.Id && c.CompanyName != model.CompanyName  && c.CompanyAddress != model.CompanyAddress);
                if(other != null)
                {
                    return new OperationResult(OperationResultType.Warning, "修改信息已存在，请重新输入！");
                }
                company.CompanyName = model.CompanyName.Trim();
                company.CompanyAddress = model.CompanyAddress.Trim();
                //company.Code = model.Code.Trim();
                company.UpdateDate = DateTime.Now;
                _companyRepository.Update(company);
                return new OperationResult(OperationResultType.Success, "更新数据成功！");
            }
            catch
            {
                return new OperationResult(OperationResultType.Error, "更新数据失败！");
            }
        }

        public OperationResult Delete(IEnumerable<CompanyVM> list)
        {
            try
            {
                if (list != null)
                {
                    var companyIds = list.Select(c => c.Id).ToList();
                    int count = _companyRepository.Entities.Where(c => companyIds.Contains(c.Id)).Delete();
                    if (count > 0)
                    {
                        return new OperationResult(OperationResultType.Success, "删除成功！");
                    }
                    else
                    {
                        return new OperationResult(OperationResultType.Error, "删除失败，请重试！");
                    }
                }
                else
                {
                    return new OperationResult(OperationResultType.ParamError, "参数错误，请选择需要删除的数据！");
                }
            }
            catch
            {
                return new OperationResult(OperationResultType.Error, "删除失败，请重试！");
            }
        }

        //public OperationResult ResetPassword(IEnumerable<CompanyVM> list)
        //{
        //    var listIds = list.Select(c => c.Id).ToList();
        //    try
        //    {
        //        string md5Pwd = EncryptionHelper.GetMd5Hash("123456");
        //        _companyRepository.Entities.Where(u => listIds.Contains(u.Id)).Update(u => new Company() { Password = md5Pwd});
        //        return new OperationResult(OperationResultType.Success, "密码重置成功！ ");
        //    }
        //    catch
        //    {
        //        return new OperationResult(OperationResultType.Error, "密码重置失败，请重试！");
        //    }
        //}
    }
}
